package cn.lunatic.utils;

import java.io.File;
import java.util.List;

import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import cn.lunatic.xmlpares.JdomXMLUtil;

public class XML {
	
	public static void main(String[] args) {
		Element e = addDir("G:/Java/BaseJava/XMLAnalytical/config");
		System.out.println(JdomXMLUtil.formatXml(e));
		System.out.println(e.getChildren().size());
		System.out.println("3333333333333");
	}
	
	public static Element addDir(String dir) {
		if (!StringUtils.isBlank(dir)) {
			File configFile = new File(dir);
			if (!configFile.exists()) {
				System.out.println("文件" + "：(" + dir + ") 不存在");
				throw new RuntimeException("文件" + "：(" + dir + ") 不存在");
			}
			Element pp = recuAddDir(new File(dir));
			System.out.println(JdomXMLUtil.formatXml(pp));
			System.out.println("00000000000");
			return pp;
		} else {
			throw new RuntimeException("文件不能为空串！");
		}
	}

	/**
	 * @Description:递归查询目录添加XML文件
	 * @author ganfeng
	 * @date 2014-3-7 上午11:47:34
	 * @param dir
	 */
	private static Element recuAddDir(File dir) {
		Element root = null;
		File[] listFiles = dir.listFiles();
		for (int i = 0; i < listFiles.length; i++) {
			File file = listFiles[i];
			if (file.isDirectory()) {// 如果是目录，继续递归
				root = recuAddNode(root,recuAddDir(file));
			} else if (file.isFile()) {// 如果是XML文件
				String filePath = file.getPath();
				if (filePath.endsWith(".xml")) {
					System.out.println("add file: " + filePath);
					root = recuAddNode(root, parseFile(filePath));
				}
			}
		}
		System.out.println(JdomXMLUtil.formatXml(root));
		System.out.println("2222222222222");
		return root;
	}

	public static Element parseFile(String xmlFile) {
		SAXBuilder builder = new SAXBuilder();
		try {
			return builder.build(xmlFile).getRootElement();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	private static Element recuAddNode(Element eMain, Element eNew) {
		if(eMain == null){
			return eNew;
		}
		List<Element> children = eNew.getChildren();
		int i = 0;
		for (;;) {
			if (i >= children.size()) {// 检查节点是否遍历完
				break;
			}
			// 取主类的克隆子类
			Element child = children.get(i);
			Element mainCloneChild = getMainCloneChild(eMain, child);
			if (mainCloneChild != null) {// 如果存在克隆子类,则递归进子类重新比较,否则,直接将子节点加入主节点树
				i++;
				recuAddNode(mainCloneChild, child);
			} else {// 注意: 节点分离后,children.size()会发生变化
				child.detach();
				eMain.addContent(child);
			}
		}
		return eMain;
	}

	private static Element getMainCloneChild(Element mainParent, Element node) {
		List<Element> children = mainParent.getChildren();
		for (int i = 0; i < children.size(); i++) {
			Element child = children.get(i);
			if (child.getName().equalsIgnoreCase(node.getName())) {
				if (!child.getName().equals(node.getName())) {
					System.out.println("存在大小写不一致的同名节点：" + child.getName() + " <--> "
							+ node.getName());
				}
				if (child.getAttributes().size() == 0) {
					return child;
				} else {
					String childId = child.getAttribute("id").getValue();
					String nodeId = node.getAttribute("id").getValue();
					if (childId == null || nodeId == null) {
						return null;
					}
					childId = childId.trim();
					nodeId = nodeId.trim();
					if (childId.equals(nodeId)) {
						System.out.println("存在元素id相同的配置：" + childId);
					}
				}
			}
		}
		return null;

	}

}
